﻿#if UNITY_EDITOR 
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
/// <summary>
/// 引擎Asset资源
/// </summary>
public class ESF_BuildEngineAsset : AbsESF_EditorBuildAsset
{
    #region 构造函数
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="_fileName">文件名称</param>
    /// <param name="_directory">目录</param>
    /// <param name="_ext">后缀</param>
    /// <param name="_className">类名称</param>
    public ESF_BuildEngineAsset(string _fileName, string _directory,
        enESF_UnityFileExt _ext, string _className)
        : base(_fileName, _directory, _ext)
    {
        SetClassName(_className);
    }
    #endregion

    #region public 属性
    /// <summary>
    /// 类名称
    /// </summary>
    public string className { get; private set; }
    /// <summary>
    /// 引擎资源
    /// </summary>
    public UnityEngine.Object engineAsset{ get; private set; }
    #endregion

    #region SetClassName 设置类名称
    /// <summary>
    /// 设置类名称
    /// </summary>
    /// <param name="_className">类名称</param>
    public void SetClassName(string _className)
    {
        className = _className;
    }
    #endregion

    #region OnCreateAsset 创建资源
    /// <summary>
    /// 创建资源
    /// </summary>
    protected override void OnCreateAsset()
    {
        switch (ext)
        {
            case enESF_UnityFileExt.Prefab:
                #region Prefab
                GameObject winPrefab = new GameObject(fileName);
                GameObject emptyPrefab = PrefabUtility.SaveAsPrefabAsset(winPrefab,filePath);
                UnityEngine.Object.DestroyImmediate(winPrefab);
                #endregion
                break;
            default:
                #region 默认
                Assembly assembly = null;
                Type type = ESF_Utility.GetType(className, ref assembly);
                if (type != null)
                {
                    UnityEngine.Object obj = (UnityEngine.Object)assembly.CreateInstance(type.FullName);
                    if (obj != null)
                    {
                        AssetDatabase.CreateAsset(obj, filePath);
                        AssetDatabase.SaveAssets();
                        //EditorGUIUtility.PingObject(obj);
                    }
                    else
                    {
                        throw new NullReferenceException(string.Format("Assembly【{0}】can not find type 【{1}】.", assembly.FullName, type.FullName));
                    }
                }
                else
                {
                    Debug.LogErrorFormat("Can not find 【{0}】asset script.", className);
                }
                #endregion
                break;
        }
    }
    #endregion

    #region OnLoadAsset 加载资源
    /// <summary>
    /// 加载资源
    /// </summary>
    protected override void OnLoadAsset()
    {
        engineAsset = null;
        switch (ext)
        {
            case enESF_UnityFileExt.Prefab:
                #region Prefab
                engineAsset = AssetDatabase.LoadAssetAtPath<GameObject>(filePath);
                #endregion
                break;
            default:
                #region 默认
                Assembly assembly = null;
                Type type = ESF_Utility.GetType(className, ref assembly);
                if (type != null)
                {
                    engineAsset = AssetDatabase.LoadAssetAtPath(filePath, type);
                }
                else
                {
                    Debug.LogErrorFormat("Can not find 【{0}】asset in path 【{1}】.", className, filePath);
                }
                #endregion
                break;
        }
    }
    #endregion
}
#endif